package ${controllerPkg};
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bjtct.oom.common.exception.BzException;
import com.bjtct.oom.common.vo.LEVEL;
import com.bjtct.oom.common.vo.Message;
<#if servicePkg?? >
import ${servicePkg}.${name?cap_first}Service;
<#else>
import ${defaultPkg!}.service.${name?lower_case}.${name?cap_first}Service;
</#if>
<#if domainPkg?? >
import ${domainPkg}.${name?cap_first}Entity;
import ${domainPkg}.${name?cap_first}Query;
<#else>
import ${defaultPkg!}.domain.${name?lower_case}.${name?cap_first}Entity;
import ${defaultPkg!}.domain.${name?lower_case}.${name?cap_first}Query;
</#if>
<#assign gentime = .now>
/**
*${name}模块对应的控制类
*@since ${gentime?string['yyyy-MM-dd HH:mm']}
*@author autogen
*/
@Controller()
@RequestMapping("/${name?lower_case}")
public class ${name?cap_first}Controller {
	public static Logger log = LoggerFactory.getLogger(${name?cap_first}Controller.class);
	@Autowired
	private ${name?cap_first}Service ${name?uncap_first}Service;
	/**
	* 主页面
	*/
	@RequestMapping("/index")
	public String index(){
		return "${name}/${name?cap_first}Index";
	}
<#if showStyle=="TREE"||showStyle=="TREE_TABLE">
	/**
	* 查询下属节点信息
	*@param ${pkCol.column_alias} 主键
	*@return json 集合信息
	*/
	@RequestMapping("/children")
	@ResponseBody
	<#if pkCol.data_type=="int">
	public Object children( Integer ${pkCol.column_alias}){
		if( null == ${pkCol.column_alias} ){
			${pkCol.column_alias}=0;
		}
	<#elseif pkCol.data_type=="long">
	public Object children( Long ${pkCol.column_alias}){
		if( null == ${pkCol.column_alias} ){
			${pkCol.column_alias}=0l;
		}
	<#else> 
	public Object children( ${pkCol.data_type}  ${pkCol.column_alias}){
		
	</#if>

		
		return ${name?uncap_first}Service.get${name?cap_first}Childrens( ${pkCol.column_alias} );
	}
	
	/**
	* 创建树
	*@return json 集合信息
	*/
	@RequestMapping("/tree")
	@ResponseBody
	public Object tree( ){
		
		return ${name?uncap_first}Service.get${name?cap_first}Tree();
	}	
</#if>
	/**
	* 根据参数查询列表信息
	*@param query 查询对象
	*@return json
	*/
	@RequestMapping("/list")
	@ResponseBody
	public Object list(${name?cap_first}Query query){
	
		return ${name?uncap_first}Service.get${name?cap_first}List(query).getResult();
	}
	/**
	* 根据参数查询统计信息
	*@param query 查询对象
	*@return json 数量
	*/
	@RequestMapping("/count")
	@ResponseBody
	public Object count(${name?cap_first}Query query){
	
		return ${name?uncap_first}Service.get${name?cap_first}Count(query);
	}
	/**
	* 根据参数查询列表信息
	*@param query 查询对象
	*@return json
	*/
	@RequestMapping("/all")
	@ResponseBody
	public Object all(${name?cap_first}Query query){	
		return ${name?uncap_first}Service.get${name?cap_first}All(query);
	}
	
	/**
	* 根据参数查询详细信息
	*@param ${pkCol.column_alias} 主键
	*@return json 详细信息
	*/
	@RequestMapping("/detail")
	@ResponseBody
	public Object detail( ${pkCol.data_type} ${pkCol.column_alias} ){
	
		return ${name?uncap_first}Service.get${name?cap_first}ByID(${pkCol.column_alias});
	}
	/**
	* 新增一条${name}记录
	*@param entity ${name}实体信息
	*@return json 操作结果
	*/
	@RequestMapping("/save")
	@ResponseBody
	public Object save(${name?cap_first}Entity entity){
		Message msg = new Message();
		try{
			
			${name?uncap_first}Service.save${name?cap_first}(entity);
			msg.setLevel(LEVEL.INFO);
			msg.setMsg("保存成功");
		}catch(Exception e){
			msg.setLevel(LEVEL.ERROR);
			msg.setSuccess(false);
			msg.setMsg(e.getMessage());
		}
		return msg;
	}
	/**
	* 修改一条${name}记录
	*@param entity ${name}实体信息
	*@return json 操作结果
	*/
	@RequestMapping("/update")
	@ResponseBody
	public Object update(${name?cap_first}Entity entity){
		Message msg = new Message();
		try{
			
			${name?uncap_first}Service.update${name?cap_first}(entity);
			msg.setLevel(LEVEL.INFO);
			msg.setMsg("修改成功！");
		}catch(Exception e){
			msg.setLevel(LEVEL.ERROR);
			msg.setSuccess(false);
			msg.setMsg(e.getMessage());
		}
		return msg;
	}
	/**
	* 删除 ${name}记录
	*@param pk  主键数组
	*@return json 操作结果
	*/
	@RequestMapping("/del")
	@ResponseBody
	public Object del(@RequestParam(value = "pk[]") ${pkCol.data_type}[] pk){
		Message msg = new Message();
		try {
			${name?uncap_first}Service.delete${name?cap_first}(pk);
			 msg.setLevel(LEVEL.INFO);
			 msg.setMsg("删除成功!");
		} catch (BzException e) {
			msg.setMsg(e.getMessage());
			msg.setLevel(LEVEL.ERROR);
			msg.setSuccess(false);
			log.error(e.getMessage());
		}
		return msg;
	}
}